'''
Author: mypx
Date: 2025-10-21 15:29:08
LastEditTime: 2025-10-21 15:32:40
LastEditors: mypx mypx_coder@163.com
Description: 
'''
# 注释模块
from PyQt6.QtWidgets import QWidget, QLabel, QHBoxLayout
from PyQt6.QtCore import Qt
from typing import Dict
from parser.config_model import CommentItem


class CommentModule:
    """注释模块"""

    def __init__(self):
        self.comment_labels: Dict[str, QLabel] = {}

    def create_comment(
        self, comment_item: CommentItem, parent: QWidget = None
    ) -> QWidget:
        """创建注释"""
        container = QWidget(parent)
        container.setObjectName(f"comment-container-{comment_item.id}")

        layout = QHBoxLayout(container)
        layout.setContentsMargins(8, 4, 8, 4)

        label = QLabel(f"# {comment_item.text}")
        label.setStyleSheet("color: #666666; font-style: italic;")
        label.setWordWrap(True)
        label.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        layout.addWidget(label)

        self.comment_labels[comment_item.id] = label

        # 设置初始可见性
        if comment_item.condition:
            self.update_comment_visibility(
                comment_item.id, comment_item.condition.evaluated
            )

        return container

    def update_comment_visibility(self, comment_id: str, visible: bool) -> None:
        """更新注释可见性"""
        label = self.comment_labels.get(comment_id)
        if label:
            label.setVisible(visible)
